package cn.edu.nju.software.service.impl;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.edu.nju.software.bean.storage.Book;
import cn.edu.nju.software.dao.intf.BookDao;
import cn.edu.nju.software.service.intf.BookService;
import cn.edu.nju.software.util.Constants;
import cn.edu.nju.software.util.PreferencesOperator;

public class BookServiceImpl implements BookService {
	
	private BookDao bookDao;
	private PreferencesOperator preferencesOperator;

	public void setBookDao(BookDao bookDao) {
		this.bookDao = bookDao;
	}

	public void setPreferencesOperator(PreferencesOperator preferencesOperator) {
		this.preferencesOperator = preferencesOperator;
	}

	@Override
	public List<Book> getAllBooks() throws SQLException {
		return bookDao.getAllBooks();
	}

	@Override
	public List<Book> getHotBooks() throws SQLException, ParseException {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String[] keys = new String[] {Constants.HOTBOOKS_DATE, Constants.HOTBOOKS_THRESHOLD};
		String[] values = preferencesOperator.getProperties(keys);
		Date date = dateFormat.parse(values[0]);
		int threshold = Integer.parseInt(values[1]);
		return bookDao.getHotBooks(date, threshold);
	}

	@Override
	public List<Book> getRecentBooks() throws SQLException, ParseException {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String[] keys = new String[] {Constants.RECENTBOOKS_DATE};
		String[] values = preferencesOperator.getProperties(keys);
		Date date = dateFormat.parse(values[0]);
		return bookDao.getRecentBooks(date);
	}

	@Override
	public List<Book> searchBooks(String isbn, String title, String author, String publisher, 
			Date beginDate, Date endDate, int category) throws SQLException {
		Map<Integer, Object> criteria = new HashMap<Integer, Object>();
		if (isbn != null && !isbn.equals("")) {
			criteria.put(Constants.SEARCH_ISBN, isbn);
		}
		if (title != null && !title.equals("")) {
			criteria.put(Constants.SEARCH_TITLE, title);
		}
		if (author != null && !author.equals("")) {
			criteria.put(Constants.SEARCH_AUTHOR, author);
		}
		if (publisher != null && !publisher.equals("")) {
			criteria.put(Constants.SEARCH_PUBLISHER, publisher);
		}
		if (beginDate != null || endDate != null) {
			if (beginDate == null) {
				beginDate = new Date(Long.MIN_VALUE);
			}
			if (endDate == null) {
				endDate = new Date(Long.MAX_VALUE);
			}
			List<Date> date = new ArrayList<Date>(2);
			date.add(beginDate);
			date.add(endDate);
			criteria.put(Constants.SEARCH_DATE, date);
		}
		if (category > 1) {
			criteria.put(Constants.SEARCH_CATEGORY, category);
		}
		return bookDao.searchBooks(criteria);
	}

	@Override
	public int addBook(Book book) throws SQLException {
		if (book.getPicPath().equals("")) {
			book.setPicPath("img/book/book_img.jpg");
		}
		if (book.getAttachPath().equals("")) {
			book.setAttachPath("null");
		}
		return bookDao.addBook(book);
	}

	@Override
	public boolean deleteBook(int bookID) throws SQLException {
		return bookDao.deleteBook(bookID);
	}

	@Override
	public int modifyBookInfo(Book book) throws SQLException {
		return bookDao.updateBook(book);
	}

	@Override
	public Book getBookById(int bookID) throws SQLException {
		return bookDao.getBookById(bookID);
	}

}
